package com.vision.shop.model;


import java.util.ArrayList;
import java.util.List;

public class ShopCart {
	public ShopCart(){
		products=new ArrayList<CartProduct>();
	}
	
	List<CartProduct> products;

	public List<CartProduct> getProducts() {
		return products;
	}

	public void setProducts(List<CartProduct> products) {
		this.products = products;
	}
	public void delete(int pid){
		for(int i=0;i<products.size();i++){
			CartProduct cp=products.get(i);
			if(cp.getPid()==pid){
				products.remove(cp);
			}
		}
		if(products.size()<=0){
			isEmpty=true;
		}
	}
	/**
	 * 清空购物车
	 */
	public void clearShopCart(){
		products.clear();
		isEmpty=true;
		
	}
	public void add(Product product){
		CartProduct cp=findCartProduct(product.getId());
		if(cp==null){
			if(product.getStock()<1){
				throw new ShopException("该商品库存不够");
			}
			cp=new CartProduct();
			cp.setPid(product.getId());
			cp.setNumber(1);
			cp.setPrice(product.getPrice());
			cp.setProduct(product);
			products.add(cp);
		}else{
			if(product.getStock()<cp.getNumber()+1){
				throw new ShopException("该商品库存不够");
			}
			cp.setNumber(cp.getNumber()+1);
			cp.setPrice(cp.getPrice()+product.getPrice());
		}
		isEmpty=false;
	}
	
	/**
	 * 检查是否有这个产品
	 */
	private CartProduct findCartProduct(int pid){
		for(CartProduct cp:products){
			if(cp.getPid()==pid){
				return cp;
			}
		}
		return null;
	}
	/*
	 * 初始化为空
	 */
	private boolean isEmpty=true;

	public boolean getisEmpty() {
		return isEmpty;
	}

	public void setisEmpty(boolean isEmpty) {
		this.isEmpty = isEmpty;
	}

	public void addProductNumber(int pid, int number) {
		for(CartProduct cp:products){
			if(cp.getPid()==pid){
				if(cp.getProduct().getStock()<cp.getNumber()+number){
					//库存不够
					throw new ShopException("商品库存不够");
				}
				cp.setNumber(cp.getNumber()+number);
				cp.setPrice(cp.getPrice()+(number*cp.getProduct().getPrice()));
			}
		}
		
	}

	
	
	
}
